package cn.com.algery.temp.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyFormAuthFilter extends FormAuthenticationFilter {

    private static final Logger log = LoggerFactory.getLogger(FormAuthenticationFilter.class);
    
    /**
     * When access is denied, if it is an OPTIONS request, it will return 200 OK directly
     * Reason: OPTIONS request will not bring login information.
     */
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
		HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        if("OPTIONS".equals(req.getMethod())) {
        	res.setStatus(200);
        	return false; // False means no further operation
        }
        if (isLoginRequest(request, response)) {
            if (isLoginSubmission(request, response)) {
                if (log.isTraceEnabled()) {
                    log.trace("Login submission detected.  Attempting to execute login.");
                }
                return executeLogin(request, response);
            } else {
                if (log.isTraceEnabled()) {
                    log.trace("Login page view.");
                }
                //allow them to see the login page ;)
                return true;
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to access a path which requires authentication.  Forwarding to the " +
                        "Authentication url [" + getLoginUrl() + "]");
            }

            saveRequestAndRedirectToLogin(request, response);// Asynchronous request can be considered modified here, instead of returning JSON
            return false;
        }
    }
//   //when ajax
//	@Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception
//
//
//@Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//        if(this.isLoginRequest(request, response)) {
//            if(this.isLoginSubmission(request, response)) {
//                return this.executeLogin(request, response);
//            } else {
//
//
//                return true;
//            }
//        } else {
//            if(isAjax(request)){
//                Map<String,Object> result=new HashMap<String,Object>();
//                result.put("isLogin",false);
//                response.getWriter().print(JsonUtils.Bean2Json(result));
//            }else{
//                this.saveRequestAndRedirectToLogin(request, response);
//            }
//            return false;
//        }
//    }
//
//
//    public static boolean isAjax(ServletRequest request){
//        String header = ((HttpServletRequest) request).getHeader("X-Requested-With");
//        if("XMLHttpRequest".equalsIgnoreCase(header)){
//            System.out.println( "The current request is an Ajax request");
//            return Boolean.TRUE;
//        }
//        System.out.println(  "Current request is non-Ajax request");
//        return Boolean.FALSE;
//    }	
	
}
